package com.example.controller;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.*;
import com.example.service.IFamilyService;
import com.example.service.IFirstService;
import com.example.service.IOutcomeService;
import com.example.service.ISecondService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 收入表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-29
 */
@RestController
@RequestMapping("/outcome")
@Slf4j
@RequiredArgsConstructor
@CrossOrigin
public class OutcomeController {

    private final IOutcomeService iOutcomeService;

    private final IFirstService iFirstService;

    private final ISecondService iSecondService;

    private final IFamilyService iFamilyService;

    @GetMapping("/page")
    public Result<PageResult> page(OutcomePage outcomePage){
        Page<Outcome> page = Page.of(outcomePage.getPage(),outcomePage.getPageSize());
        page.addOrder(new OrderItem()
                .setAsc(false)
                .setColumn("time"));
        iOutcomeService.lambdaQuery()
                .eq(outcomePage.getTypeKey1() != null,Outcome::getTypeKey1,outcomePage.getTypeKey1())
                .eq(outcomePage.getTypeKey2() != null,Outcome::getTypeKey2,outcomePage.getTypeKey2())
                .like(outcomePage.getMember() != null,Outcome::getMember,outcomePage.getMember())
                .like(outcomePage.getDescription() != null,Outcome::getDescription,outcomePage.getDescription())
                .between(outcomePage.getBegin() != null && outcomePage.getEnd() != null,Outcome::getTime,outcomePage.getBegin(),outcomePage.getEnd())
                .page(page);
        List<OutcomeVO> list = page.getRecords().stream().map(outcome -> {
            OutcomeVO outcomeVO = new OutcomeVO();
            BeanUtils.copyProperties(outcome, outcomeVO);
            outcomeVO.setTypeName1(iFirstService.getById(outcome.getTypeKey1()).getTypeName());
            if(outcome.getTypeKey2() != null)outcomeVO.setTypeName2(iSecondService.getById(outcome.getTypeKey2()).getTypeName());
            outcomeVO.setMemberName(iFamilyService.getById(outcome.getMember()).getName());
            return outcomeVO;
        }).collect(Collectors.toList());
        return Result.success(new PageResult(page.getTotal(),list));
    }

    @PutMapping
    public Result update(@RequestBody Outcome outcome){
        iOutcomeService.updateById(outcome);
        return Result.success();
    }

    @PostMapping("/save")
    public Result save(@RequestBody Outcome outcome){
        outcome.setCreateTime(LocalDateTime.now());
        iOutcomeService.save(outcome);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result delete(Long id){
        iOutcomeService.removeById(id);
        return Result.success();
    }

    @GetMapping("/detailedOutcome")
    public Result<List<Double>> details(@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate begin,
                                        @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end){
        log.info("details收到的参数有：{}，{}",begin,end);
        List<Double> list = iOutcomeService.details(begin,end);
        return Result.success(list);
    }

    @GetMapping("/classifiedOutcome")
    public Result<List<COutcome>> classified(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                             @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        List<COutcome> list = iOutcomeService.classified(begin,end);
        return Result.success(list);
    }

}
